<template>
  <div class="section-container">
    <div class="section-header">
      <div class="title-wrapper">
        <h2 class="title">{{ title }}</h2>
        <!-- 通过 showInfoIcon 控制图标的显示 -->
        <div
          v-if="showInfoIcon"
          class="info-icon"
          @mouseenter="handleEnter"
          @mouseleave="handleLeave"
        >
          <img src="@/assets/info.png" />
          <transition name="fade">
            <div v-show="isInfoVisible" class="tooltip">
              <div v-if="tooltipTitle" class="tooltip-title">
                {{ tooltipTitle }}
              </div>
              <div
                v-for="(content, index) in tooltipContent"
                :key="index"
                class="tooltip-text"
              >
                {{ content }}
              </div>
              <!-- 左侧小箭头元素 -->
              <div class="tooltip-arrow"></div>
            </div>
          </transition>
        </div>
      </div>
      <div class="collapse-btn" @click="toggleCollapse">
        <img src="@/assets/collap.png" alt="Collapse panel" />
      </div>
    </div>
  </div>
</template>

<script>
export default {
  props: {
    title: String,
    tooltipTitle: String,
    tooltipContent: {
      type: Array,
      default: () => [],
    },
    showInfoIcon: {
      // 新增属性控制图标显示
      type: Boolean,
      default: true,
    },
    panelIndex: {
      type: Number,
      required: false,
    },
    isExpanded: {
      type: Boolean,
      default: false,
    },
  },
  data() {
    return {
      isInfoVisible: false,
    };
  },
  methods: {
    handleEnter() {
      if (this.tooltipContent == "") return;
      this.isInfoVisible = true;
    },
    handleLeave() {
      this.isInfoVisible = false;
    },
    toggleCollapse() {
      const newState = !this.isExpanded;
      this.$emit("toggle-collapse", {
        panelIndex: this.panelIndex,
        isExpanded: newState,
      });
    },
  },
};
</script>

<style scoped lang="scss">
.section-container {
  display: flex;
  align-items: center;

  .section-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    width: 100%;

    .title-wrapper {
      display: flex;
      align-items: center;

      .title {
        font-size: 0.1389rem;
        font-weight: 700;
        color: #0d4c80;
      }

      .info-icon {
        position: relative;
        width: 0.1389rem;
        height: 0.1389rem;
        margin-left: 0.0694rem;
        cursor: url("@/assets/cursor3.png"), auto;
        img {
          width: 100%;
          height: 100%;
        }
      }
    }

    .collapse-btn {
      width: 0.2083rem;
      height: 0.2083rem;

      img {
        width: 100%;
        height: 100%;
        cursor: url("@/assets/cursor3.png"), auto;
      }
    }
  }

  .tooltip {
    position: absolute;
    top: 0%;
    left: 50px;
    width: 1.5972rem;
    min-width: 1.5972rem;
    background: white;
    border: 1px solid #ddd;
    padding: 0.0417rem;
    box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
    z-index: 10;
    word-spacing: 1px;
    font-family: "Inter Variable", sans-serif;
    /* 为左侧箭头留出空间 */
    padding-left: 0.0833rem;
  }

  .tooltip-title {
    font-family: "Inter Variable", sans-serif;
    font-size: 10px;
    font-weight: 600;
    color: #454545;
    margin-bottom: 4px;
  }

  .tooltip-text {
    font-family: "Inter Variable", sans-serif;
    font-size: 0.0833rem;
    font-weight: 400;
    color: #6a6969;
    line-height: 1.4;
  }

  // .tooltip-arrow {
  //   position: absolute;
  //   top: 0%; /* 垂直居中 */
  //   left: 0; /* 最左侧 */
  //   // transform: translateY(-50%); /* 垂直居中调整 */
  //   width: 4px;
  //   height: 4px;
  //   /* 箭头指向左侧，与弹窗左边框连接 */
  //   border-top: 5px solid transparent;
  //   border-bottom: 5px solid transparent;
  //   border-right: 5px solid #ddd; /* 与弹窗边框颜色一致，指向右侧 */
  //   /* 向左偏移5px，使箭头一半超出弹窗 */
  //   margin-left: -5px;
  // }

  .fade-enter-active,
  .fade-leave-active {
    transition: opacity 0.3s;
  }
  .fade-enter,
  .fade-leave-to {
    opacity: 0;
  }
}
</style>
